package com.sino.util;

/**
 * <p>Title: ���ڹ����ࡣ</p>
 * <p>Description: �ṩ�����ڴ���ĺ���</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author Alwin
 * @version 1.1 2003-06-04
 */

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

public class DateUtils {
	public static final String FORMAT_DATE_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	public static final String FORMAT_DATE_YYYY_MM_DD = "yyyy-MM-dd";

	public static final String FORMAT_DATE_YYYY = "yyyy";

	public static final String TYPE_YEAR = "y";

	public static final String TYPE_MONTH = "m";

	public static final String TYPE_DATE = "d";

	public static void main(String[] args) {
		// Date date=new Date(long date);
		// System.out.println(date);
		// System.out.println(DateUtils.formatDateTime(date,
		// FORMAT_DATE_YYYY_MM_DD));

//		Calendar cal = Calendar.getInstance();
//		 int year = cal.get(Calendar.YEAR);
//		 int month = (cal.get(Calendar.MONTH))+1;
//		 int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
//		 System.out.println(year);
//		 System.out.println(month);
//		 System.out.println(day_of_month);
		Calendar   c   =   Calendar.getInstance();   
		  Date   d   =   c.getTime();
		  Date date=new Date();
		  System.out.println(d);
		  System.out.println(date);

	}

	/**
	 * ���ܣ���ȡϵͳ��ǰ������--sql���ڶ���
	 * 
	 * @return java.sql.Date
	 */
	public static final java.sql.Date getSystemDate() {
		long now = System.currentTimeMillis();
		java.sql.Date vDate = new java.sql.Date(now);
		return vDate;
	}

	/**
	 * ���ܣ���ȡϵͳ��ǰ������ʱ���--sql���ڶ���
	 * 
	 * @return java.sql.Timestamp
	 */
	public static final java.sql.Timestamp getTimestamp() {
		long now = System.currentTimeMillis();
		return (new java.sql.Timestamp(now));
	}

	/**
	 * ���ܣ���ʽ�����ڣ�����һ��java.util.Date�����ϵͳ��ʽ���ַ��������ڶ����ʽ������ַ����
	 * 
	 * @param vDate:(���ڶ���java.util.Date)
	 * @param formatStr:ϵͳ��ʽ���ַ�
	 * @return �����ַ�
	 */
	public static final String formatDateTime(java.util.Date vDate,
			final String formatStr) {
		if (vDate == null)
			return null;
		java.text.SimpleDateFormat formatter;
		formatter = new java.text.SimpleDateFormat(formatStr);
		String dateString = formatter.format(vDate);
		return dateString;
	}

	public static final Date formatDate(java.util.Date vDate,
			final String formatStr) {
		if (vDate == null)
			return null;
		java.text.SimpleDateFormat formatter;
		formatter = new java.text.SimpleDateFormat(formatStr);
		String dateString = formatter.format(vDate);
		return formatDateTime(dateString, formatStr);
	}

	/**
	 * ���ܣ��������ַ��ʽ�������ڶ���
	 * 
	 * @param vDate:
	 *            �����ַ�String
	 * @param formatStr:ϵͳ��ʽ���ַ�
	 * @return ���ڶ���java.lang.Date
	 */
	public static final java.util.Date formatDateTime(String vDate,
			final String formatStr) {
		if (vDate == null)
			return null;
		if (vDate.equals(""))
			return null;
		try {
			java.text.SimpleDateFormat sdf;
			sdf = new SimpleDateFormat(formatStr);
			java.util.Date uDate = sdf.parse(vDate);
			return uDate;
		} catch (Exception e) {
			return null;
		}
	}

	public static final String formatStringDateTime(String vDate,
			final String formatStr) {
		if (vDate == null)
			return null;
		if (vDate.equals(""))
			return null;

		try {
			java.text.SimpleDateFormat sdf;
			sdf = new SimpleDateFormat(formatStr);
			java.util.Date uDate = sdf.parse(vDate);
			String s = StringUtils.dateToMillis(uDate);
			return s;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * ���ܣ�ת������ʱ���ַ�ת�������ڶ���
	 * 
	 * @param sDate
	 *            :����ʱ���ַ�
	 * @return Date :���ڶ��� java.sql.Date
	 */
	public static final java.sql.Date toDateTime(String sDate) {
		java.util.Date uDate = new java.util.Date();
		if (formatDateTime(sDate, DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS) != null) {
			uDate = formatDateTime(sDate,
					DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS);
		} else if (formatDateTime(sDate, DateUtils.FORMAT_DATE_YYYY_MM_DD) != null) {
			uDate = formatDateTime(sDate, DateUtils.FORMAT_DATE_YYYY_MM_DD);
		} else {
			uDate = null;
		}
		if (uDate != null) {
			String s = StringUtils.dateToMillis(uDate);
			long now = Long.parseLong(s);
			java.sql.Date sqlDate = new java.sql.Date(now);
			return sqlDate;
		}
		return null;
	}

	/**
	 * ���ܣ�������ʱ���ַ�ת��������ʱ�����
	 * 
	 * @param sDate
	 *            :(����ʱ���ʽ���ַ�)
	 * @return java.sql.Timestamp :����ʱ������
	 */
	public static final java.sql.Timestamp toTimestamp(String sDate) {
		java.util.Date uDate = new java.util.Date();
		if (formatDateTime(sDate, DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS) != null) {
			uDate = formatDateTime(sDate,
					DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS);
		} else if (formatDateTime(sDate, DateUtils.FORMAT_DATE_YYYY_MM_DD) != null) {
			uDate = formatDateTime(sDate, DateUtils.FORMAT_DATE_YYYY_MM_DD);
		} else {
			uDate = null;
		}
		if (uDate != null) {
			String s = StringUtils.dateToMillis(uDate);
			long now = Long.parseLong(s);
			java.sql.Timestamp sqlDate = new java.sql.Timestamp(now);
			return sqlDate;
		}
		return null;
	}

	/**
	 * ���ܣ���ȡϵͳʱ��,���ص�ʱ���ַ��ʽ(YYYY-MM-DD
	 * HH:MM:SS)��
	 * 
	 * @return java.lang.String
	 */
	public static String getCurrDate() {
		java.util.Date currDate = new java.util.Date();
		String dateString = null;
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		dateString = formatter.format(currDate);
		return dateString;
	}

	/**
	 * ���ܣ���ȡϵͳ����,���ص������ַ��ʽ(YYYY-MM-DD)��
	 * 
	 * @return java.lang.String
	 */
	public static String getDate() {
		java.util.Date currDate = new java.util.Date();
		String dateString = null;
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		dateString = formatter.format(currDate);
		return dateString;
	}

	/**
	 * ���ܣ���ȡϵͳʱ��,���ص�ʱ���ַ��ʽΪָ���ĸ�ʽ
	 * 
	 * @param formatStr
	 *            ��(����ʱ��ĸ�ʽ)
	 * @return java.lang.String �����������ַ�
	 */
	public static String getCurrDate(final String formatStr) {
		java.util.Date currDate = new java.util.Date();
		String dateString = null;
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				formatStr);
		dateString = formatter.format(currDate);
		return dateString;
	}

	/**
	 * ���ܣ���ȡϵͳʱ������ʱ��,
	 * 
	 * @param type
	 *            ���������͡�y���꣬m�� d ��
	 * @param numDat
	 *            :����ʱ�䳤��
	 * @return java.lang.String :����ָ���������ַ�
	 */
	public static String reverDate(String type, int numDat) {
		java.util.Calendar cal = java.util.Calendar.getInstance();
		if (type.toLowerCase().equals("y")) {
			cal.add(java.util.Calendar.YEAR, numDat);
		}
		if (type.toLowerCase().equals("m")) {
			cal.add(java.util.Calendar.MONTH, numDat);
		}
		if (type.toLowerCase().equals("d")) {
			cal.add(java.util.Calendar.DAY_OF_MONTH, numDat);
		}

		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		String dateString = formatter.format(cal.getTime());
		return dateString;
	}

	/**
	 * ���ܣ���ȡָ��ʱ��ļ������
	 * 
	 * @param date
	 *            ��ָ��������ʱ��
	 * @param type
	 *            ����������y=�꣬m=��d=�գ�numDat����ʱ�䳤��
	 * @param numdat
	 *            ������
	 * @return �����������ַ�
	 */
	public static String reverDate(Date date, final String type, int numdat) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if (type.toLowerCase().equals("y")) {
			calendar.add(Calendar.YEAR, numdat);
		}
		if (type.toLowerCase().equals("m")) {
			calendar.add(Calendar.MONTH, numdat);
		}
		if (type.toLowerCase().equals("d")) {
			calendar.add(Calendar.DAY_OF_YEAR, numdat);
		}
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		return formatter.format(calendar.getTime());
	}

	/**
	 * ���ܣ����}�����ڼ���ʱ��
	 * 
	 * @param startDate
	 *            ����ʼ����
	 * @param endDate
	 *            ����������
	 * @param type
	 *            ����������y=�꣬m=�� d=��
	 * @return java.lang.Integer ��������������
	 */
	public static int diffdates(Date startDate, Date endDate, final String type) {
		int result = 0;
		Calendar startCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		startCalendar.setTime(startDate);
		endCalendar.setTime(endDate);

		if (type.toLowerCase().equals("y")) {
			result = endCalendar.get(Calendar.YEAR)
					- startCalendar.get(Calendar.YEAR);
		}
		if (type.toLowerCase().equals("m")) {
			int years = endCalendar.get(Calendar.YEAR)
					- startCalendar.get(Calendar.YEAR);
			result = 12
					* years
					+ (endCalendar.get(Calendar.MONTH) - startCalendar
							.get(Calendar.MONTH));
		}
		if (type.toLowerCase().equals("d")) {
			long startTime = startCalendar.getTimeInMillis();
			long endTime = endCalendar.getTimeInMillis();
			result = Integer.parseInt(Long.toString((endTime - startTime)
					/ (1000 * 60 * 60 * 24)));
		}
		return result;
	}
}
